package com.sh.data.engine.domain.normalization.modeldesign.busmatrix.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.data.engine.domain.normalization.modeldesign.busmatrix.model.domain.LogicRelFieldDomain;
import com.sh.data.engine.domain.normalization.modeldesign.busmatrix.service.BusMatrixFieldService;
import com.sh.data.engine.repository.dao.normalization.modeldesign.busmatrix.BusMatrixFieldMapper;
import com.sh.data.engine.repository.dao.normalization.modeldesign.busmatrix.arg.SaveLogicRelArg;
import com.sh.data.engine.repository.dao.normalization.modeldesign.busmatrix.entity.MatrixFieldEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : changkong
 * @date : 2022/8/15
 */
@Slf4j
@Service
public class BusMatrixFieldServiceImpl extends ServiceImpl<BusMatrixFieldMapper, MatrixFieldEntity>
    implements BusMatrixFieldService {

    @Override
    public List<LogicRelFieldDomain> getLogicRelFields(Long matrixId) {
        List<MatrixFieldEntity> result =
            this.list(
                new LambdaQueryWrapper<MatrixFieldEntity>()
                    .eq(MatrixFieldEntity::getMatrixId, matrixId)
                    .eq(MatrixFieldEntity::getRowState, 1));
        return result.stream()
            .map(
                i ->
                    LogicRelFieldDomain.builder()
                        .factField(i.getFactField())
                        .dimField(i.getDimField())
                        .build())
            .collect(Collectors.toList());
    }

    @Override
    public boolean cancelLogicRelFields(Long matrixId) {
        return this.update(
            new LambdaUpdateWrapper<MatrixFieldEntity>()
                .eq(MatrixFieldEntity::getMatrixId, matrixId)
                .set(MatrixFieldEntity::getRowState, 0));
    }

    @Override
    public boolean saveLogicRelFields(Long matrixId, List<SaveLogicRelArg.RelField> fields) {
        // 清空之前的字段设置
        this.update(
            new LambdaUpdateWrapper<MatrixFieldEntity>()
                .eq(MatrixFieldEntity::getMatrixId, matrixId)
                .set(MatrixFieldEntity::getRowState, 0));

        int size = fields.size();
        List<MatrixFieldEntity> fieldEntityList = new ArrayList<>(size);
        for (SaveLogicRelArg.RelField field : fields) {
            MatrixFieldEntity entity =
                MatrixFieldEntity.builder()
                    .matrixId(matrixId)
                    .factField(field.getFactField())
                    .dimField(field.getDimField())
                    .rowState(1)
                    .build();
            fieldEntityList.add(entity);
        }
        return this.saveBatch(fieldEntityList);
    }
}
